package com.ruoyi.web.controller.carte;

import java.util.List;

import com.ruoyi.system.domain.XxkCarte;
import com.ruoyi.system.domain.request.CarteStatusRequest;
import com.ruoyi.system.domain.request.JobStatusRequest;
import com.ruoyi.system.domain.request.TransStatusRequest;
import com.ruoyi.system.service.carte.job.IJobStatus;
import com.ruoyi.system.service.carte.trans.ITransStatus;
import com.ruoyi.system.xxkutil.XxkUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.XxkCarteRuns;
import com.ruoyi.system.service.IXxkCarteRunsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * carte服务器运行转换情况Controller
 * 
 * @author ruoyi
 * @date 2025-02-23
 */
@Controller
@RequestMapping("/system/runs")
public class XxkCarteRunsController extends BaseController
{
    private String prefix = "system/runs";

    @Autowired
    private IXxkCarteRunsService xxkCarteRunsService;

    @Autowired
    private IJobStatus jobStatus;

    @Autowired
    private ITransStatus transStatus;

    @RequiresPermissions("system:runs:view")
    @GetMapping()
    public String runs()
    {
        return prefix + "/runs";
    }

    /**
     * 查询carte服务器运行转换情况列表
     */
    @RequiresPermissions("system:runs:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(XxkCarteRuns xxkCarteRuns)
    {
        startPage();
        List<XxkCarteRuns> list = xxkCarteRunsService.selectXxkCarteRunsList(xxkCarteRuns);
        return getDataTable(list);
    }

    /**
     * 导出carte服务器运行转换情况列表
     */
    @RequiresPermissions("system:runs:export")
    @Log(title = "carte服务器运行转换情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(XxkCarteRuns xxkCarteRuns)
    {
        List<XxkCarteRuns> list = xxkCarteRunsService.selectXxkCarteRunsList(xxkCarteRuns);
        ExcelUtil<XxkCarteRuns> util = new ExcelUtil<XxkCarteRuns>(XxkCarteRuns.class);
        return util.exportExcel(list, "carte服务器运行转换情况数据");
    }

    /**
     * 新增carte服务器运行转换情况
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存carte服务器运行转换情况
     */
    @RequiresPermissions("system:runs:add")
    @Log(title = "carte服务器运行转换情况", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(XxkCarteRuns xxkCarteRuns)
    {
        return toAjax(xxkCarteRunsService.insertXxkCarteRuns(xxkCarteRuns));
    }

    /**
     * 修改carte服务器运行转换情况
     */
    @RequiresPermissions("system:runs:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        XxkCarteRuns xxkCarteRuns = xxkCarteRunsService.selectXxkCarteRunsById(id);
        mmap.put("xxkCarteRuns", xxkCarteRuns);
        return prefix + "/edit";
    }

    /**
     * 修改保存carte服务器运行转换情况
     */
    @RequiresPermissions("system:runs:edit")
    @Log(title = "carte服务器运行转换情况", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(XxkCarteRuns xxkCarteRuns)
    {
        return toAjax(xxkCarteRunsService.updateXxkCarteRuns(xxkCarteRuns));
    }

    /**
     * 删除carte服务器运行转换情况
     */
    @RequiresPermissions("system:runs:remove")
    @Log(title = "carte服务器运行转换情况", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(xxkCarteRunsService.deleteXxkCarteRunsByIds(ids));
    }


    @RequiresPermissions("system:runs:edit")
    @GetMapping("/serverRunsInfo/{id}")
    public String serverRunsInfo(@PathVariable("id") Long id,ModelMap mmap)
    {
        XxkCarteRuns xxkCarteRuns = xxkCarteRunsService.selectXxkCarteRunsById(id);
        if("job".equalsIgnoreCase(xxkCarteRuns.getItemType())){
            JobStatusRequest request = new JobStatusRequest();
            request.setName(xxkCarteRuns.getName());
            request.setCarteCode(xxkCarteRuns.getCarteCode());
            request.setId(xxkCarteRuns.getCarteObjectId());
            request.setXml("N");
            String result = jobStatus.jobStatus(request);
            String baseUrl = "http://"+xxkCarteRuns.getCarteIp()+":"+xxkCarteRuns.getCartePort();
            String modifiedHtml = XxkUtils.replaceRelativeUrls(result, baseUrl);
            modifiedHtml = modifiedHtml.replaceAll("/kettle", baseUrl + "/kettle");
            mmap.put("serverRunsInfo",modifiedHtml);
        }else{
//            TransStatusRequest request = new TransStatusRequest();
//            request.setName(xxkCarteRuns.getName());
//            request.setCarteCode(xxkCarteRuns.getCarteCode());
//            request.setId(xxkCarteRuns.getCarteObjectId());
//            request.setXml("N");
//            String result = transStatus.transStatus(request);
//            String baseUrl = "http://"+xxkCarteRuns.getCarteIp()+":"+xxkCarteRuns.getCartePort();
//            String modifiedHtml = XxkUtils.replaceRelativeUrls(result, baseUrl);
//            modifiedHtml = modifiedHtml.replaceAll("/kettle", baseUrl + "/kettle");
//            mmap.put("serverRunsInfo",modifiedHtml);
            //exectrans 执行之后carte服务不返回carteObjectId，所以不能直接定位日志
            mmap.put("serverRunsInfo","execTrans执行之后，carte服务不返回carteObjectId，所以无法直接定位日志");
        }
        return prefix + "/carteServerRunsInfo";
    }
}
